//
//  MarkaCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/1/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit



protocol ModelCellData {
    var isSelected: Bool { get set }
    var name: String { get }
    
    // after name, to show generation of serie, and serie of modification
    var postfix: String? { get set }
}

final class ModelCell: UITableViewCell {
    
    // MARK: - UI
    
    lazy private var itemView: ModelItem = {
        let iView = ModelItem(isExpandedItem: false)
        iView.layer.borderWidth = 1
        iView.layer.borderColor = UIColor(red: 229/255, green: 233/255, blue: 242/255, alpha: 1).cgColor
        iView.clipsToBounds = true
        iView.layer.cornerRadius = 10.0
        return iView
    }()
    
    // MARK: - Props
    
    var model: ModelCellData! {
        didSet {
            self.setUpCell(old: oldValue)
        }
    }
    
    // MARK: - Init
   
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.setUpView()
    }
    
    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        let size = super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority)
        return CGSize(width: size.width, height: max(size.height, 80))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - Single Selection Mode
    
    public func handleSingleSelection (markIcon: UIImage?) {
        self.itemView.layer.borderWidth = 0
        itemView.addArrow()
        itemView.addIcon(image: markIcon)
    }
    
    public func fillImageWithColor (_ color: UIColor) {
        itemView.fillCircular(color)
    }
    
    
    private func handleArrowIcon () {
        itemView.addArrow()
    }
}

extension ModelCell {
    fileprivate func setUpCell (old: ModelCellData?) {
        if model != nil {
            self.itemView.updateText(title: model.name)
        }
        self.configureRow(old: old)
    }
    
    fileprivate func configureRow (old: ModelCellData?) {
        if (model != nil && (old == nil || old!.isSelected != model.isSelected)) {
            if (model.isSelected) {
                self.itemView.didSelectCheckbox()
            } else {
                self.itemView.didDeselectCheckbox()
            }
        }
    }
}



// MARK: - Configuring UI

extension ModelCell {
    fileprivate func setUpView() {
        self.contentView.addSubview(itemView)
        self.addConstraints()
        
    }
    
    fileprivate func addConstraints () {
        itemView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(8)
            make.leading.trailing.equalToSuperview()
            make.height.equalTo(64)
        }
    }
}




